package com.myBlog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.myBlog.domain.ResponseResult;
import com.myBlog.domain.dto.TagListDTO;
import com.myBlog.domain.entity.ArticleTag;
import com.myBlog.domain.entity.Tag;
import com.myBlog.domain.vo.PageVo;
import com.myBlog.domain.vo.TagVo;
import com.myBlog.enums.AppHttpCodeEnum;
import com.myBlog.exception.SystemException;
import com.myBlog.mapper.TagMapper;
import com.myBlog.service.ArticleTagService;
import com.myBlog.service.TagService;
import com.myBlog.utils.BeanCopyUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 标签(Tag)表服务实现类
 *
 * @author makejava
 * @since 2023-03-18 19:49:34
 */
@Service
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag> implements TagService {
    @Resource
    private ArticleTagService articleTagService;

    /**
     * 分页查询标签
     *
     * @param pageNum
     * @param pageSize
     * @param tagListDTO
     * @return
     */
    @Override
    public ResponseResult<PageVo> list(Integer pageNum, Integer pageSize, TagListDTO tagListDTO) {
        //添加查询条件
        LambdaQueryWrapper<Tag> wrapper = new LambdaQueryWrapper<>();
        Page<Tag> tagPage = new Page<>(pageNum, pageSize);
        wrapper.like(StringUtils.hasText(tagListDTO.getName()), Tag::getName, tagListDTO.getName());
        wrapper.like(StringUtils.hasText(tagListDTO.getRemark()), Tag::getRemark, tagListDTO.getRemark());
        //分页查询
        new Page<Tag>(pageNum, pageSize);
        page(tagPage, wrapper);
        //封装数据返回
        PageVo pageVo = new PageVo(tagPage.getRecords(), tagPage.getTotal());
        return ResponseResult.okResult(pageVo);
    }

    /**
     * 新增标签
     *
     * @param tagListDTO
     * @return
     */
    @Override
    public ResponseResult addTag(TagListDTO tagListDTO) {

        if (!StringUtils.hasText(tagListDTO.getName())) {
            throw new SystemException(AppHttpCodeEnum.TAGNAME_NOT_NULL);
        }
        Tag tag = BeanCopyUtils.copyBean(tagListDTO, Tag.class);
        this.save(tag);
        return ResponseResult.okResult();
    }

    /**
     * 根据id删除标签
     *
     * @param ids
     * @return
     */
    @Override
    public ResponseResult deleteById(List<Long> ids) {
//        List<ArticleTag> articleTags = articleTagService.listByIds(ids);
       /* if (articleTags.size() > 0) {
            throw new SystemException(AppHttpCodeEnum.TAG_HAS_LINK);
        } else {
            List<Tag> tagList = listByIds(ids);
            tagList = tagList.stream()
                    .peek(tag -> tag.setDelFlag(1))
                    .collect(Collectors.toList());

            removeBatchByIds(tagList);
        }*/
        List<Tag> tagList = listByIds(ids);
        tagList = tagList.stream()
                .peek(tag -> tag.setDelFlag(1))
                .collect(Collectors.toList());
        removeBatchByIds(tagList);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getTagInfo(Long tagId) {
        Tag tag = getById(tagId);
        TagListDTO tagListDTO = new TagListDTO(tag.getId(), tag.getName(), tag.getRemark());
        return ResponseResult.okResult(tagListDTO);
    }

    /**
     * 修改标签
     *
     * @param tagListDTO
     * @return
     */
    @Override
    public ResponseResult updateTag(TagListDTO tagListDTO) {

        if ((null == tagListDTO.getId()) || !StringUtils.hasText(tagListDTO.getName())) {
            throw new SystemException(AppHttpCodeEnum.TAGNAME_NOT_NULL);
        }
        Tag tag = BeanCopyUtils.copyBean(tagListDTO, Tag.class);
        updateById(tag);
        return ResponseResult.okResult();
    }

    /**
     * 查询全部标签
     *
     * @return
     */
    @Override
    public List<TagVo> listAllTag() {
        LambdaQueryWrapper<Tag> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(Tag::getId, Tag::getName);
        List<Tag> tagList = list(wrapper);
        List<TagVo> tagVos = BeanCopyUtils.copyBeanList(tagList, TagVo.class);
        return tagVos;
    }
}

